package com.iknow.audio;

import com.iknow.IKnow;
import com.iknow.file.CacheSystem;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.util.IKFileUtil;
import com.iknow.xmpp.service.AudioMessage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.List;

public class MessageAudioManager
{
  private MessageAudioCallback mCallback;
  private MessageDigest mDigest;
  private AudioTask mTask;
  private TaskListener mTaskListener;
  private List<AudioMessage> mUrlList;

  public MessageAudioManager(MessageAudioCallback paramMessageAudioCallback)
  {
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "AudioTask";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        super.onPostExecute(paramGenericTask, paramTaskResult);
        if ((TaskResult.OK == paramTaskResult) && (MessageAudioManager.access$0(this.this$0).size() > 0))
          MessageAudioManager.access$1(this.this$0, null);
      }

      public void onProgressUpdate(, Object paramObject)
      {
        super.onProgressUpdate(paramGenericTask, paramObject);
        AudioMessage localAudioMessage = (AudioMessage)((TaskParams)paramObject).get("msg");
        if (MessageAudioManager.access$2(this.this$0) != null)
          MessageAudioManager.access$2(this.this$0).refreash(localAudioMessage);
      }
    };
    this.mCallback = paramMessageAudioCallback;
  }

  private void doDownloadFile(AudioMessage paramAudioMessage)
  {
    List localList = this.mUrlList;
    monitorenter;
    if (paramAudioMessage != null);
    try
    {
      return;
    }
    finally
    {
      while (true)
      {
        monitorexit;
        throw localObject;
        this.mTask = new AudioTask(this, null);
        this.mTask.setListener(this.mTaskListener);
        this.mTask.execute(new TaskParams[0]);
      }
    }
  }

  private String getHashString(MessageDigest paramMessageDigest)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    byte[] arrayOfByte = paramMessageDigest.digest();
    int i = arrayOfByte.length;
    int j = 0;
    while (true)
    {
      if (j >= i)
        return localStringBuilder.toString();
      int k = arrayOfByte[j];
      localStringBuilder.append(Integer.toHexString(0xF & k >> 4));
      localStringBuilder.append(Integer.toHexString(k & 0xF));
      ++j;
    }
  }

  private String getMd5String(String paramString)
  {
    this.mDigest.update(paramString.getBytes());
    return getHashString(this.mDigest);
  }

  private String lookup(String paramString)
  {
    String str2;
    String str1 = getMd5String(paramString);
    if (new File(IKnow.mCacheSystem.getAudioPath() + str1).exists())
      str2 = IKnow.mCacheSystem.getAudioPath() + str1;
    while (true)
    {
      while (true)
      {
        return str2;
        if (!(new File(paramString).exists()))
          break;
        str2 = paramString;
      }
      str2 = null;
    }
  }

  private boolean write(String paramString, File paramFile)
  {
    int i = 0;
    File localFile = new File(IKnow.mCacheSystem.getAudioPath() + getMd5String(paramString));
    try
    {
      localFile.createNewFile();
      IKFileUtil.writeFile(new FileInputStream(paramFile), localFile);
      i = 1;
      return i;
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
    }
  }

  public String getAudioFilePath(AudioMessage paramAudioMessage)
  {
    String str2;
    String str1 = lookup(paramAudioMessage.getFileName());
    if (str1 != null)
      str2 = str1;
    while (true)
    {
      return str2;
      doDownloadFile(paramAudioMessage);
      str2 = null;
    }
  }

  private class AudioTask extends GenericTask
  {
    // ERROR //
    protected TaskResult _doInBackground()
    {
      // Byte code:
      //   0: aload_0
      //   1: getfield 13	com/iknow/audio/MessageAudioManager$AudioTask:this$0	Lcom/iknow/audio/MessageAudioManager;
      //   4: invokestatic 25	com/iknow/audio/MessageAudioManager:access$0	(Lcom/iknow/audio/MessageAudioManager;)Ljava/util/List;
      //   7: invokeinterface 31 1 0
      //   12: ifgt +7 -> 19
      //   15: getstatic 37	com/iknow/task/TaskResult:OK	Lcom/iknow/task/TaskResult;
      //   18: areturn
      //   19: aload_0
      //   20: getfield 13	com/iknow/audio/MessageAudioManager$AudioTask:this$0	Lcom/iknow/audio/MessageAudioManager;
      //   23: invokestatic 25	com/iknow/audio/MessageAudioManager:access$0	(Lcom/iknow/audio/MessageAudioManager;)Ljava/util/List;
      //   26: astore_2
      //   27: aload_2
      //   28: monitorenter
      //   29: aload_0
      //   30: getfield 13	com/iknow/audio/MessageAudioManager$AudioTask:this$0	Lcom/iknow/audio/MessageAudioManager;
      //   33: invokestatic 25	com/iknow/audio/MessageAudioManager:access$0	(Lcom/iknow/audio/MessageAudioManager;)Ljava/util/List;
      //   36: iconst_0
      //   37: invokeinterface 41 2 0
      //   42: checkcast 43	com/iknow/xmpp/service/AudioMessage
      //   45: astore 4
      //   47: aload_0
      //   48: getfield 13	com/iknow/audio/MessageAudioManager$AudioTask:this$0	Lcom/iknow/audio/MessageAudioManager;
      //   51: invokestatic 25	com/iknow/audio/MessageAudioManager:access$0	(Lcom/iknow/audio/MessageAudioManager;)Ljava/util/List;
      //   54: iconst_0
      //   55: invokeinterface 46 2 0
      //   60: pop
      //   61: aload_2
      //   62: monitorexit
      //   63: aload 4
      //   65: ifnonnull -65 -> 0
      //   68: goto -68 -> 0
      //   71: astore_3
      //   72: aload_2
      //   73: monitorexit
      //   74: aload_3
      //   75: athrow
      //
      // Exception table:
      //   from	to	target	type
      //   29	74	71	finally
    }
  }
}